package com.flutter.aliyun_push

import android.content.Context
import android.util.Log
import com.alibaba.sdk.android.push.MessageReceiver
import com.alibaba.sdk.android.push.notification.CPushMessage
import android.os.Handler
import android.os.Looper

/**
 * Date: 9/26/21
 * Time: 5:28 PM
 * author: liMing
 */
class AliYunPushIntentService : MessageReceiver() {
    private val handler=Handler(Looper.myLooper()!!)
    val tAG = "receiver"
    /*用于接收服务端推送的通知，通知会弹窗，通知接收通知以及点击通知等都有回调。
       通知接收回调
        客户端接收到通知后，回调该方法。
        可获取到并处理通知相关的参数。
     */
    override fun onNotification(context: Context?, title: String, summary: String, extraMap: Map<String?, String?>) {
        // TODO 处理推送通知
        Log.e("MyMessageReceiver", "Receive notification, title: $title, summary: $summary, extraMap: $extraMap")
        handler.post {
            AliYunPushHandler.methodChannel?.invokeMethod("onNotification", mapOf(
                    "title" to title,
                    "summary" to summary,
                    "extraMap" to extraMap
            ))
        }
    }

    //用于接收服务端推送的消息，消息不会弹窗，而是回调该方法。
    override fun onMessage(context: Context?, cPushMessage: CPushMessage) {
        Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.messageId + ", title: " + cPushMessage.title + ", content:" + cPushMessage.content)
        handler.post {
            AliYunPushHandler.methodChannel?.invokeMethod("onMessageArrived", mapOf(
                    "appId" to cPushMessage.appId,
                    "messageId" to cPushMessage.messageId,
                    "title" to cPushMessage.title,
                    "content" to cPushMessage.content,
                    "traceInfo" to cPushMessage.traceInfo
            ))
        }
    }

    //通知打开回调 打开通知时会回调该方法，通知打开上报由SDK自动完成。
    override fun onNotificationOpened(context: Context?, title: String, summary: String, extraMap: String) {
        Log.e("MyMessageReceiver", "onNotificationOpened, title: $title, summary: $summary, extraMap:$extraMap")
        handler.post {
            AliYunPushHandler.methodChannel?.invokeMethod("onNotificationOpened", mapOf(
                    "title" to title,
                    "summary" to summary,
                    "extraMap" to extraMap
            ))
        }
    }

    //无跳转逻辑通知打开回调 打开无跳转逻辑(open=4)通知时回调该方法(v2.3.2及以上版本支持)，通知打开上报由SDK自动完成。
    override fun onNotificationClickedWithNoAction(context: Context?, title: String, summary: String, extraMap: String) {
        Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: $title, summary: $summary, extraMap:$extraMap")
        handler.post {
            AliYunPushHandler.methodChannel?.invokeMethod("onNotificationOpened", mapOf(
                    "title" to title,
                    "summary" to summary,
                    "extraMap" to extraMap
            ))
        }
    }

    override fun onNotificationReceivedInApp(context: Context?, title: String, summary: String, extraMap: Map<String?, String?>, openType: Int, openActivity: String, openUrl: String) {
        Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: $title, summary: $summary, extraMap:$extraMap, openType:$openType, openActivity:$openActivity, openUrl:$openUrl")
        handler.post {
            AliYunPushHandler.methodChannel?.invokeMethod("onNotificationOpened", mapOf(
                    "title" to title,
                    "summary" to summary,
                    "extras" to extraMap,
                    "openType" to openType,
                    "openActivity" to openActivity,
                    "openUrl" to openUrl
            ))
        }
    }

    override fun onNotificationRemoved(context: Context?, messageId: String?) {
        Log.e("MyMessageReceiver", "onNotificationRemoved")
        handler.post {
            AliYunPushHandler.methodChannel?.invokeMethod("onNotificationOpened", mapOf(
                    "messageId" to messageId
            ))
        }
    }
}